<?php 
	include("session.php");
	include("util/message.php");
	include("util/constant.php");
	include_once("service/userService.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Profile | Cart</title>
<?php include("import.php");?>
</head>

<body>
<!-- Wrapper -->
<div class="wrapper">
	<!-- Floating banner -->
    <?php include("floating-banner.php");?>
    <!-- End of floating banner -->
    
	<!-- Top banner -->
	<?php include("header.php");?>
    <!-- End of top banner -->
    
    <!-- Navigator -->
	<?php include("navigator.php");?>
    <!-- End of navigator -->
    
    <!-- Menu, newsletter and search -->
    <?php include("menu.php");?>
    <!-- End of menu, newsletter and search -->
    
    <!-- Content -->
    <div class="wrapper-content no-background">
    	<?php
			$class = "";
			$message = "";
			if(isset($_GET["action"])){
				$action = $_GET["action"];
							
				if($action == INSERT_FAILED){
					$message = $messages["createReceiptFailed"];
					$class = "err";
				} else if($action == INSERT_SUCCESSFULLY){
					$message = $messages["createReceiptSuccessfully"];
					$class = "suc";
				} else if($action == UPDATE_SUCCESSFULLY.".1"){
					$message = $messages["editProfileSuccessfully"];
					$class = "suc";
				} else if($action == UPDATE_FAILED.".1"){
					$message = $messages["editProfileFailed"];
					$class = "err";
				} else if($action == UPDATE_SUCCESSFULLY.".2"){
					$message = $messages["changePasswordSuccessfully"];
					$class = "suc";
				} else if($action == UPDATE_FAILED.".2"){
					$message = $messages["changePasswordFailed"];
					$class = "err";
				}		
        ?>
	    	<div id="msg"><div class="<?php echo $class;?>"><?php echo $message;?></div></div>
        <?php
			}
        ?>
    	<!-- Tabs -->
        <div class="tabs-wrapper">
            <ul id="tabs" class="tabs">
            	<?php if(isset($_SESSION["username"])){?>
	                <li id="profileTab" class="active"><a href="#profile">Thông tin cá nhân</a></li>
                <?php
					}
                ?>
                <li id="cartTab"><a href="#cart">Giỏ hàng của tôi</a></li>
                <li id="deliveryTab"><a href="#deliveryInfo">Thông tin giao hàng</a></li>
            </ul>
            <?php 
				if(isset($_SESSION["username"])){
					$userInfo = mysql_fetch_array(findUserById($_SESSION["username"][0]));
			?>
                <div id="profile" class="tab-content">
                    <!-- Profile wrapper -->
                    <form method="post" action="action/editProfileAction.php" id="profileForm">
                        <div class="profile-wrapper">
                            <div class="control-block">
                                <label class="control-label w125">Tên thành viên</label>
                                <input type="text" class="w185" name="fullName" value="<?php echo $userInfo["USER_FULLNAME"];?>"/>
                            </div>
                            <div class="control-block">
                                <label class="control-label w125">Email</label>
                                <label><?php echo $userInfo["USER_EMAIL"];?></label>
                            </div>
                            <div class="control-block">
                                <label class="control-label w125">Nickname</label>
                                <label><?php echo $userInfo["USER_USERNAME"];?></label>   
                            </div>
                            <div class="control-block">
                                <label class="control-label w125">Địa chỉ giao hàng</label>
                                <input type="text" class="w185" name="profileAddress" value="<?php echo $userInfo["USER_ADDRESS"];?>"/>   
                            </div>
                            <div class="control-block">
                                <label class="control-label w125">Số điện thoại</label>
                                <label><?php echo $userInfo["USER_PHONENUMBER"];?></label> 
                            </div>
                            <div class="control-block">
                                <label class="control-label w125">Ngày sinh</label>
                                <label><?php echo date_format(date_create($userInfo["USER_DOB"]), "d/m/Y");?></label>   
                            </div>
                            <div class="control-block">
                                <label class="control-label w125">Giới tính</label>
                                <input type="radio" name="gender" value="1" <?php echo($userInfo["USER_GENDER"] == 1) ? "checked='checked'" : ""?>/> Nam
                                <input type="radio" name="gender" value="0" <?php echo($userInfo["USER_GENDER"] == 0) ? "checked='checked'" : ""?>/> Nữ
                            </div>
                        </div>
                    
                        <!-- End of profile wrapper -->
                        
                        <div class="profile-wrapper-sep"></div>
                        
                        <div class="change-pass-wrapper">
                            <span>
                                <input type="checkbox" name="changePassMode" id="changePassMode" onclick="changeMode();" /> THAY ĐỔI MẬT KHẨU
                            </span>
                            <div class="control-block">
                                <label class="control-label w125">Mật khẩu hiện tại</label>
                                <input type="password" class="w200" placeholder="Vui lòng nhập mật khẩu hiện tại" name="currentPassword"/>   
                            </div>
                            <div class="control-block">
                                <label class="control-label w125">Mật khẩu mới</label>
                                <input type="password" class="w200" placeholder="Vui lòng nhập mật khẩu mới" name="newPassword"/>   
                            </div>
                            <div class="control-block">
                                <label class="control-label w125">Xác nhận mật khẩu</label>
                                <input type="password" class="w200" placeholder="Vui lòng xác nhận mật khẩu mới" name="confirmNewPassword"/>   
                            </div>
                            <div class="control-block">
                                <input type="submit" class="save-profile-btn" value="Lưu thay đổi"/>   
                            </div>
                        </div>
                    </form>
	            </div>
            <?php
				}
			?>
            <form id="form" method="post" action="action/releaseCartAction.php">
                <div id="cart" class="tab-content">
                    <?php include("cart.php");?>
                </div>
                <div id="deliveryInfo" class="tab-content">
                    <div class="control-block">
                        <label class="control-label w125">Số điện thoại</label>
                        <input type="text" class="w185" placeholder="Vui lòng nhập số điện thoại" name="phoneNumber"/>   
                    </div>
                    <div class="control-block ">
                        <label class="control-label w125">Địa chỉ giao hàng</label>
                        <input type="text" class="w435" placeholder="Vui lòng nhập địa chỉ giao hàng" name="deliveryingAddress"/>   
                    </div>
                    <div class="control-block ">
                        <label class="control-label w125">Địa chỉ trả tiền</label>
                        <input type="text" class="w435" placeholder="Vui lòng nhập địa chỉ trả tiền" name="payingAddress"/>   
                    </div>
                    <div class="control-block ">
                        <label class="control-label w125">Ngày giao hàng</label>
                        <div class="input-append date datepicker" data-date-format="dd/mm/yyyy" data-date="">
                            <input class="span2" type="text" readonly="" size="16" name="deliveryingDate"/>
                            <span class="add-on"><i class="icon-calendar"></i></span>
                        </div>   
                    </div>
                    <input class="login-btn" type="button" onclick="backStep();" value="Trở về">
                    <input class="login-btn" type="<?php if(isset($_SESSION["username"])) echo "submit"; else echo "button"?>" id="checkout" value="Hoàn tất">
                </div>
            </form>
			<script>
				
				$(document).ready(function() {
					changeMode();
					
					<?php
						if(isset($_SESSION["username"])){
					?>	
							var isValidPass = false;
							$.validator.addMethod("validateConfirmPass", function(value) {
								return ($("input[name^=newPassword]").val() == $("input[name^=confirmNewPassword]").val());
							}, "<?php echo $messages["validation.notMatchPassword"]; ?>");
							
							$.validator.addMethod("checkPassword", function(value, element) {
								$.ajax({
									type: "POST",
									url: "action/ajax/checkPasswordProcess.php",
									data: {"pass" : $("input[name^=currentPassword]").val()},
									success: function(data){
										if(data == 1) {
											isValidPass = true;
										}
										else {
											//$.validator.messages.checkPassword = "<?php echo $messages["validation.wrongPassword"];?>";
											isValidPass = false;
										}   
									}
								});
								return isValidPass;
							
							}, "<?php echo $messages["validation.wrongPassword"];?>");
							
							//if($("#changePassMode").attr('checked')){
								$("#profileForm").validate({
									rules: {
										currentPassword: { checkPassword: true },
										newPassword: { required: true },
										confirmNewPassword: { validateConfirmPass: true },
									},
									onfocusout: false,
									onkeyup: false,
									messages: {
										newPassword: "<?php echo $messages["validation.required"];?>"
									}
								});
							//}
					<?php
						}
					?>
					$("#checkout").click(function(){
						<?php 
							if(isset($_SESSION["username"])){
						?>
						$("#form").validate({
							rules: {
								phoneNumber: { required: true },
								deliveryingAddress: { required: true },
								payingAddress: { required: true },
								deliveryingDate: { required: true }
							},
							
							messages: {
								phoneNumber: "<?php echo $messages["validation.required"];?>",
								deliveryingAddress: "<?php echo $messages["validation.required"];?>",
								payingAddress: "<?php echo $messages["validation.required"];?>",
								deliveryingDate: "<?php echo $messages["validation.required"];?>"
							}
						});
						<?php
							} else{
						?>
							submitAction('register.php');
						<?php
							}
						?>
					});
					
				});
				
                function backStep(){
					submitAction('profile-cart.php#cart-tab');
                    $("#deliveryTab").css("display", "none");
                }
				
				function changeMode(){
					if($("#changePassMode").attr('checked')){
						$("input[name^=currentPassword]").removeAttr("disabled");
						$("input[name^=newPassword]").removeAttr("disabled");
						$("input[name^=confirmNewPassword]").removeAttr("disabled");
					} else {
						$("input[name^=currentPassword]").attr("disabled", "disabled");
						$("input[name^=newPassword]").attr("disabled", "disabled");
						$("input[name^=confirmNewPassword]").attr("disabled", "disabled");
						
					}
				}
				
            </script>
        </div>
        <!-- End of tabs -->
        <div class="clear"></div>	
    </div>
    <!-- End of content -->

    <!-- Live support -->
        <?php include("live-support.php");?>
    <!-- End of live support -->

	<?php include("footer.php");?>
</div>
<!-- End of wrapper -->

</body>
</html>